/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.dao.services.NewsDAOServices;
import model.enetities.News;
import model.enetities.NewsCategory;

/**
 *
 * @author Ku81
 */
public class NewsDAO implements NewsDAOServices{
    
    private static NewsDAO newsDAO;

    public static NewsDAO getInstance() {
        if (newsDAO == null) {
            newsDAO = new NewsDAO();
        }
        return newsDAO;
    }

    @Override
    public List<News> getNews(boolean isActived) {
        List<News> listNews = new ArrayList<News>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblNews where isActived = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);

            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                News news = new News();
                news.setIdNews(rs.getInt("idNews"));
                news.setTitle(rs.getString("title"));
                news.setContents(rs.getString("contents"));
                news.setSumary(rs.getInt("sumary"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listNews;
    }

    @Override
    public News getNewsByID(int idNews,boolean isActived) {
        News news = new News();
        try {
            Connection con = ConnectionFactory.getConnection();
            String sql = "select * from tblNews where idNews = ? and isActived = ?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, idNews);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                news.setIdNews(rs.getInt("idNews"));
                news.setTitle(rs.getString("title"));
                news.setContents(rs.getString("contents"));
                news.setSumary(rs.getInt("sumary"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return news;
    }

    @Override
    public boolean createrNews(News news) {
        boolean isCheck = false;
        try {
 
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblNews values (?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, news.getTitle());
            pstmt.setString(2, news.getContents());
            pstmt.setInt(3, news.getSumary());
            pstmt.setBoolean(4, news.isActived());
            
            pstmt.executeUpdate();
            isCheck = true;
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;

    }

    @Override
    public boolean updateNews(News news) {
        boolean isCheck = false;
        try {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            Connection con = ConnectionFactory.getConnection();
            String sql = "update tblNews set title = ?, contents = ?, sumary = ?,isActived = ? where idNews = ? ";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, news.getTitle());
            pstmt.setString(2, news.getContents());
            pstmt.setInt(3, news.getSumary());
            pstmt.setBoolean(4, news.isActived());
            pstmt.setInt(5, news.getIdNews());
            
            pstmt.executeUpdate();
            isCheck = true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;
    }

    @Override
    public boolean deleteNews(int idNews) {
        boolean isCheck = false;
        try {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            Connection con = ConnectionFactory.getConnection();
            String sql = "update tblNews set isActived ='false' where idNews = ?";
            PreparedStatement pstmt  =  con.prepareStatement(sql);
            pstmt.setInt(1, idNews);
            
            pstmt.executeUpdate();
            isCheck = true;
        } catch (SQLException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isCheck;
    }



    
}
